package main

import (
	"log"
	//"os"
	"fmt"
	"strings"
	//"math"
	"github.com/go-ini/ini"
	//	"github.com/tarm/goserial"
)

var Map07hex map[string][1024]byte

func a2h(value string) [1024]byte {
	var tmpstr [64]string
	var tmp [1024]byte
	var index int = 0

	value = strings.Replace(value, " ", "", -1)
	for _, a := range value {
		if a == 44 { //去逗号

			index = index + 1
		} else {
			tmpstr[index] = tmpstr[index] + string(a)
		}
	}
	index = 0
	fmt.Println(tmpstr)
	for _, str := range tmpstr {
		if str != "" {
			b := str2arraybyte(str)
			fmt.Println(b)
			for i := len(b) - 1; i > -1; i-- {
				//fmt.Println(len(b), i)
				tmp[index] = b[i]
				index++
			}
		}
	}
	fmt.Println(tmp[:index])
	return tmp
}

func str2arraybyte(data string) [1024]byte {
	var D = map[byte]byte{49: 1, 50: 2, 51: 3, 52: 4, 53: 5, 54: 6, 55: 7, 56: 8, 57: 9, 48: 0, 97: 10, 98: 11, 99: 12, 100: 13, 101: 14, 102: 15, 65: 10, 66: 11, 67: 12, 68: 13, 69: 14, 70: 15}
	var tmp [1024]byte
	var index int = 0

	if len(data)%2 == 1 {
		data = data + "0"
	}
	fmt.Println(data)
	bdata := []byte(data)
	fmt.Println(bdata)
	for len(bdata) > 0 {

		tmp[index] = D[bdata[0]]<<4 + D[bdata[1]] + 51
		fmt.Println(tmp)
		bdata = bdata[2:]
		index++
		//fmt.Println(tmp)
	}

	return tmp
}

func main() {
	//	c := &serial.Config{Name: "COM45", Baud: 115200}
	//	s, err := serial.OpenPort(c)
	//	if err != nil {
	//		log.Fatal(err)
	//	}

	//	n, err := s.Write([]byte("test"))
	//	if err != nil {
	//		log.Fatal(err)
	//	}

	//	buf := make([]byte, 128)
	//	n, err = s.Read(buf)
	//	if err != nil {
	//		log.Fatal(err)
	//	}
	//	log.Printf("%q", buf[:n])
	//currentDir, err := os.Getwd()
	cfg, err := ini.Load("./setup.ini")
	if err != nil {
		log.Fatal(err)
	}
	//	keys := cfg.Section("07三相表").Keys()

	//	for _, v := range keys {
	//		log.Printf("%s", v)

	//	}
	hash, err := cfg.GetSection("07三相表")
	Map07str := hash.KeysHash()

	for key, value := range Map07str {
		fmt.Println(key, value)
		Map07hex[key] = a2h(value)
	}
	fmt.Printf("%s", hash)
	log.Printf("%s", Map07str)
	for _, v := range Map07str {
		log.Printf("%s", v)
	}
	//	log.Printf("%s", keys)
	log.Printf("%q", err)
	log.Printf("%q", cfg)
	//	log.Printf("%s", errs)
}
